package com.yunxin.core.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunxin.common.exception.BusinessException;
import com.yunxin.core.admin.mapper.AdminUserMapper;
import com.yunxin.core.customer.domain.CustomerShipAllocation;
import com.yunxin.core.customer.dto.CustomerShipAllocationAddDto;
import com.yunxin.core.customer.dto.CustomerShipAllocationEditDto;
import com.yunxin.core.customer.dto.CustomerShipAllocationPageDto;
import com.yunxin.core.customer.mapper.CustomerShipAllocationMapper;
import com.yunxin.core.customer.service.CustomerShipAllocationService;
import com.yunxin.core.customer.vo.CustomerShipAllocationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author Administrator
 * @description 针对表【customer_ship_allocation(关系配置表)】的数据库操作Service实现
 * @createDate 2024-05-31 16:33:48
 */
@DS("crm")
@Service
public class CustomerShipAllocationServiceImpl extends ServiceImpl<CustomerShipAllocationMapper, CustomerShipAllocation> implements CustomerShipAllocationService {

    @Autowired
    private AdminUserMapper adminUserMapper;


    @Override
    public IPage<CustomerShipAllocationVo> customerShipAllocationPage(CustomerShipAllocationPageDto dto) {
        return baseMapper.getPageList(new Page<>(dto.getPageNum(), dto.getPageSize()), dto);

    }

    @Override
    public Boolean addCustomerShipAllocation(CustomerShipAllocationAddDto dto) {
        List<Integer> salesmanIds = dto.getSalesmanIds();
        List<CustomerShipAllocation> allocationList=new ArrayList<>();
        for (Integer salesmanId : salesmanIds) {
            List<Integer> telephoneIds = dto.getTelephoneIds();
            for (Integer telephoneId : telephoneIds) {
                LambdaQueryWrapper<CustomerShipAllocation> queryWrapper=new LambdaQueryWrapper<>();
                queryWrapper.eq(CustomerShipAllocation::getSalesmanId,salesmanId).eq(CustomerShipAllocation::getTelephoneId,telephoneId);
                CustomerShipAllocation allocation = this.getOne(queryWrapper);
                if (ObjectUtils.isEmpty(allocation)){
                    allocation = new CustomerShipAllocation();
                    allocation.setTeamId(dto.getTeamId());
                    allocation.setSalesmanId(salesmanId);
                    allocation.setTelephoneId(telephoneId);
                    allocation.setCreateTime(new Date());
                    allocation.setCreator(dto.getCreator());
                    allocationList.add(allocation);
                }
            }
        }
        if (!ObjectUtils.isEmpty(allocationList)){
            this.saveBatch(allocationList);
        }
        return true;
    }

    @Override
    public Boolean editCustomerShipAllocation(CustomerShipAllocationEditDto dto) {
        LambdaQueryWrapper<CustomerShipAllocation> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(CustomerShipAllocation::getSalesmanId,dto.getSalesmanId())
                .eq(CustomerShipAllocation::getTelephoneId,dto.getTelephoneId())
                .ne(CustomerShipAllocation::getId,dto.getId());
        CustomerShipAllocation one = this.getOne(queryWrapper);
        if (!ObjectUtils.isEmpty(one)){
            throw new BusinessException("该业务员已经绑定该话务员,请勿重复绑定");
        }
        CustomerShipAllocation allocation = new CustomerShipAllocation();
        BeanUtil.copyProperties(dto, allocation);
        try {
            this.updateById(allocation);
        } catch (Exception e) {
            throw new BusinessException("修改失败");
        }
        return true;

    }
}




